
#include "sedona.h"

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <memory.h>
#include <wchar.h>


#include <sqlite3.h>
#include <sys/time.h>
#include <time.h>


static int a=15;
static int b=15;
static int c=20;
static int e=1;
static int f=1;
static int g=1;
static int k=1;
static struct tm *pt;
static int cur_sec, cur_min, cur_hour, cur_day, cur_month, cur_year, cur_weekday;
static char sql1[500]="CREATE TABLE MyTable_1(ID integer PRIMARY KEY autoincrement,time nvarchar(100),IndoorTemp integer NOT NULL,OutdoorTemp integer NOT NULL,humidity integer NOT NULL,winderstate integer NOT NULL,conditionerstate integer NOT NULL,upload integer NOT NULL)";
static char sql2[100]="ALTER TABLE MyTable_1 RENAME TO MyTable_2";
static char sql3[100]="ALTER TABLE MyTable_2 ADD COLUMN warning integer";
static char sql4[100]="DROP TABLE MyTable_2";
static char sql5[100]="SELECT ID,upload FROM MyTable_1";
static char sql6[100];
static char sql7[200];
static char sql8[200]="obtain the system information";
static char sql9[200]="SELECT ID,upload from MyTable_1 WHERE upload=0";
static char sql10[500]="CREATE TABLE MyTable_2(ID integer PRIMARY KEY autoincrement,time nvarchar(100) UNIQUE,command text,download integer NOT NULL)";
static char sql11[200];
static char sql12[200];
static char sql13[200];
static char sql14[100]="SELECT * FROM MyTable_2";
static char sql15[100];
static char sql16[100];
static char sql17[100];
static char sql18[100];
static char sql19[100];
static char sql20[100];
static char sql21[100];
static sqlite3 *db;
static int result;
static char *errmsg = NULL;
static unsigned long size;


unsigned long get_file_size(const char *path)
{
        unsigned long filesize = -1;
        struct stat statbuff;
        if(stat(path, &statbuff) < 0){
                return filesize;
        }else{
                filesize = statbuff.st_size;
        }
      //  printf("the size of file is %ld\n",filesize);
        return filesize;
}

int generateSQL1()
{
    time_t t = time(NULL);
    pt = localtime(&t);

    cur_sec = pt->tm_sec;
    cur_min = pt->tm_min;
    cur_hour = pt->tm_hour;
    cur_day = pt->tm_mday;
    cur_month = pt->tm_mon + 1;
    cur_year = pt->tm_year + 1900;   
    cur_weekday = pt->tm_wday;  
    sprintf(sql7,"INSERT INTO MyTable_1(time,IndoorTemp,OutdoorTemp,humidity,winderstate,conditionerstate,upload) VALUES('%d-%d-%d,%d:%d:%d',%d,%d,%d,%d,%d,%d)",cur_year,cur_month,cur_day,cur_hour,cur_min,cur_sec,a,b,c,e,f,g);
g=g^0x01;
}

int generateSQL2()
{
    time_t t = time(NULL);
    pt = localtime(&t);

    cur_sec = pt->tm_sec;
    cur_min = pt->tm_min;
    cur_hour = pt->tm_hour;
    cur_day = pt->tm_mday;
    cur_month = pt->tm_mon + 1;
    cur_year = pt->tm_year + 1900;
    cur_weekday = pt->tm_wday;
    sprintf(sql11,"INSERT INTO MyTable_2(time,command,download) VALUES('%d-%d-%d,%d:%d:%d','%s',%d)",cur_year,cur_month,cur_day,cur_hour,cur_min,cur_sec,sql8,g);
}


int sqliteExecCallback(void *para, int n_column, char **column_value, char **column_name)
{
   int i;
   printf("Record contain %d field\n",n_column);
   for(i=0;i<n_column;i++)
   {
      	printf( "Name of field:%s,Value of field:%s\n",column_name[i],column_value[i]);         }
		printf( "------------------\n");
	return 0;
}



/*
Cell sqlite_SqliteDatabaseStd_sqliteCmdExecCallback(SedonaVM* vm, Cell* params)
{
	 char* cmdString1  = params[1].aval;
	// uint8_t cmdID  = params[2].ival;


	result = sqlite3_exec(db,cmdString1,sqliteExecCallback,NULL,&errmsg);

	if(result!=SQLITE_OK)
	{
       printf( "Unsuccessfully,error code:%d,Reason of error:%s\n",result,errmsg);
	   return negOneCell;
	}

	printf("==== sqlite db exec === \n"); 
	
    return zeroCell;
}
*/

Cell sqlite_SqliteDatabaseStd_sqliteCmdExec(SedonaVM* vm, Cell* params)
{
/*	 char* cmdString1  = params[1].aval;
	// uint8_t cmdID  = params[2].ival;


	result = sqlite3_exec(db,cmdString1,NULL,NULL,&errmsg);

	if(result!=SQLITE_OK)
	{
       printf( "Unsuccessfully,error code:%d,Reason of error:%s\n",result,errmsg);
	   return negOneCell;
	}

	printf("==== sqlite db exec === \n"); 
*/	
    return zeroCell;
}


Cell sqlite_SqliteDatabaseStd_sqliteCloseDb(SedonaVM* vm, Cell* params)
{
/*	 char* dbFilename  = params[1].aval;
	// char* outNameValue  = params[2].aval;
	// uint8_t* len  = params[3].aval;
	 
	sqlite3_close(db);

	printf("==== sqlite db closed === \n"); 
*/	
    return zeroCell;
}



Cell sqlite_SqliteDatabaseStd_sqliteOpenDb(SedonaVM* vm, Cell* params)
{
/*	 char* dbFilename  = params[1].aval;
	// char* outNameValue  = params[2].aval;
	// uint8_t* len  = params[3].aval;
	 
	result=sqlite3_open(dbFilename,&db);
	if(result!=SQLITE_OK)
	{
		return negOneCell;
	}

	printf("==== sqlite db opened === \n");
*/	 
	
/*	printf("Parsing the document...\n");

	for(i=0; i<250; i++)
		printf("%c",document[i]);

	printf("\n");
	


	printf("====indexParse*parseWidth= %d .\n",indexParse*parseWidth);

	for(i=0; i<indexParse*parseWidth; i++)
	{

	//	printf("%i   %c  %c  ",i,jsonStringParsed[i],outNameValue[i]);
	}
	

	len[0] = indexParse;
*/


   return zeroCell;
}



